package com.senthink.gl.business.domain.service;

import com.senthink.gl.business.common.entity.SearchRequest;
import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.domain.entity.Area;
import com.senthink.gl.business.domain.repository.AreaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 区域数据服务
 *
 * @author zooqi@senthink.com
 * @date 2017/11/22
 */
@Service
public class AreaDataService {

    @Autowired
    private AreaRepository repository;

    /**
     * 获取区域详情
     *
     * @param aid 区域唯一id
     * @return
     */
    public Optional<Area> findByAid(String aid) {
        try {
            return repository.findTopByAid(aid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取区域详情
     *
     * @param name
     * @return
     */
    public Optional<Area> findByName(String name) {
        try {
            return repository.findTopByName(name);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取一组区域详情
     *
     * @param aids
     * @return
     */
    public List<Area> findAllByAids(Iterable<String> aids) {
        try {
            return repository.findAllByAidIn(aids);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 保存/更新记录
     *
     * @param area 需要保存/更新的实体
     * @return
     */
    public Area save(Area area) {
        try {
            return repository.save(area);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 删除区域
     *
     * @param aid 区域唯一id
     * @return
     */
    public boolean deleteByAid(String aid) {
        try {
            if (repository.deleteByAid(aid) > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过name计数
     *
     * @param name
     * @return
     */
    public long countByName(String name) {
        try {
            return repository.countByName(name);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取所有区域列表
     *
     * @return
     */
    public List<Area> findAll() {
        try {
            return repository.findAll();
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取所有区域列表
     *
     * @return
     */
    public List<Area> findAll(Sort sort) {
        try {
            return repository.findAll(sort);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取所有区域列表(按区域名称排序)
     *
     * @return
     */
    public List<Area> findAllOrderByName() {
        try {
            return repository.findAllByOrderByName();
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 分页+筛选+搜索区域列表
     *
     * @param searchRequest 搜索参数
     * @param pageRequest   分页参数
     * @return
     */
    public Page<Area> findAll(PageRequest pageRequest, SearchRequest searchRequest) {
        try {
            return repository.findAll((root, query, cb) -> {
                if (searchRequest != null) {
                    return searchRequest.generatePredicate(root, cb);
                }
                return cb.conjunction();
            }, pageRequest);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
}
